package com.huawei123.hasee.cniao5store.base;

import android.os.Bundle;
import android.support.annotation.Nullable;

import com.huawei123.hasee.cniao5store.base.mvp_base.MvpBasePresenter;
import com.huawei123.hasee.cniao5store.base.mvp_base.MvpBaseView;
import com.huawei123.hasee.cniao5store.di.component.DaggerFragmentComponent;
import com.huawei123.hasee.cniao5store.di.component.FragmentComponent;
import com.huawei123.hasee.cniao5store.di.module.FragmentModule;

/**
 * 项目名称：Cniao5Store
 * 类描述：基于MVP架构的Fragment基类
 * 创建人：Administrator
 * 创建时间：2017/11/12 0012 上午 11:25
 * 修改人：Administrator
 * 修改时间：2017/11/12 0012 上午 11:25
 * 修改备注：首先实例化FragmentComponent，在根据子类返回的其Presenter绑定View，在Destroy中解绑View
 */

public abstract class BaseMvpFragment<T extends MvpBasePresenter> extends BaseFragment implements MvpBaseView {

    protected FragmentComponent fragmentComponent;
    protected T myPresenter;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initFragmentComponent();
        //根据子类返回的其Presenter绑定View
        myPresenter = initPresenterInjector();
        myPresenter.attachView(this);
    }

    /**
     * 初始化ActivityComponent
     */
    private void initFragmentComponent() {
        fragmentComponent = DaggerFragmentComponent.builder()
                .appComponent(((StoreApplication)getActivity().getApplication()).getApplicationComponent())
                .fragmentModule(new FragmentModule(this))
                .build() ;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        //解绑View
        if (myPresenter != null) {
            myPresenter.detachView();
        }
    }

    /**
     * 子类完成依赖注入并返回其Presenter
     *
     * @return
     */
    protected abstract T initPresenterInjector();

    @Override
    public void showToast(String message) {
        super.showToast(message);
    }
}
